package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.entity.XwJurisdiction;
import com.xwdeshj.repository.XwJurisdictionRepository;
import com.xwdeshj.result.IMoocJSONResult;
import com.xwdeshj.service.XwJurisdictionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

/**
 * Created by BKWJA on 2018/11/20.
 */
@Slf4j
@Service("xwJurisdictionService")
public class XwJurisdictionServiceImpl implements XwJurisdictionService {

    @Autowired
    private XwJurisdictionRepository xwJurisdictionRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public JSONObject ObtainUserJur(String userEncryptid) {
        JSONObject result = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        try {

            JSONObject result1 = new JSONObject();
            JSONArray jsonArray1 = new JSONArray();
            XwJurisdiction jurisdiction = xwJurisdictionRepository.findByUserEncryptid(userEncryptid);
            jsonArray1.add(jurisdiction);
            result1.put("channel",jsonArray1);
            System.out.println("channel:"+result1);

            String sql = "select * from xw_jurisdiction where jur_encryptId = "+userEncryptid;
            SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);

            while (rs.next()){
                JSONObject object = new JSONObject();
                object.put("jur_id",rs.getString("jur_id"));
                object.put("url",rs.getString("url"));
                object.put("title",rs.getString("jur_title"));
                object.put("interfacetitle",rs.getString("interfacetitle"));
                jsonArray.add(object);
            }
            result.put("channel",jsonArray);
        }catch (Exception e){
            e.printStackTrace();
            result.put("status","500");
            result.put("msg",e);
        }
        return result;
    }

    @Override
    public JSONObject addUserJur(String userEncryptid) {
        return null;
    }
}
